package com.itheima.task.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Component
public class XxlJobTask {


    @XxlJob("xxlJobDemo")    //这个注解跟任务管理的JobHandler对应。
    public void xxlJobDemo(){
        System.out.println(LocalDateTime.now());
    }

    @XxlJob("shardingJob")
    public void shardingJob(){
        //分片的参数
        //当前分片索引
        int shardIndex = XxlJobHelper.getShardIndex();
        //总分片数（启动了多个服务器）
        int shardTotal = XxlJobHelper.getShardTotal();

        //执行任务
        List<Integer> list = getList();
        for (Integer id : list) {
            if( id % shardTotal == shardIndex ){
                System.out.println("当前分片为："+shardIndex+",任务项为："+id);
            }
        }

    }

    public List<Integer> getList(){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i <10000; i++) {
            list.add(i);
        }
        return list;
    }
}
